﻿using System;
using System.Diagnostics;
using Excel.Expressions;
using Excel.Expressions.Compiler;
using Expressions.Compiler;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NonterminalExpressions = Excel.Expressions.Expressions;

namespace Tests
{
    /// <summary>
    ///This is a test class for Cell_AssigmentTest and is intended
    ///to contain all Cell_AssigmentTest Unit Tests
    ///</summary>
    [TestClass()]
    public class Test6
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        /// <summary>
        ///A test for Eval
        ///</summary>
        [TestMethod(), Conditional("DEBUG")]
        public void Test6_1()
        {
            Nonterminal<char> G = Nonterminals.cell_assigment_expression;
            Expression<char, CellExpression> Gexpr = NonterminalExpressions.cell_assigment_expression;
            Parser<char> input = "A0=1+1".ToCharArray();

            G.Initialize();
            G.Validate();

            G.Parse(input);
            bool expected = true;
            bool actual = (Gexpr[input.Tree] as Assignment).Expression == 2;
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        /// A test for Eval
        /// </summary>
        [TestMethod(), Conditional("DEBUG")]
        public void Test6_2()
        {
            Nonterminal<char> G = Nonterminals.cell_expression;

            G.Initialize();
            G.Validate();

            Parser<char> input = "SUM(A0,A1)".ToCharArray();
            bool expected = true;
            bool actual = G.Parse(input) == true;
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        /// A test for Eval
        /// </summary>
        [TestMethod(), Conditional("DEBUG")]
        public void Test6_3()
        {
            Nonterminal<char> G = Nonterminals.cell_expression;
            Parser<char> input = "SUM(A0)".ToCharArray();
            bool expected = true;
            bool actual = G.Parse(input) == true;
            Assert.AreEqual(expected, actual);
        }

        [TestMethod(), Conditional("DEBUG")]
        public void Test6_4()
        {
            Nonterminal<char> G = Nonterminals.cell_assigment_expression;
            Expression<char, CellExpression> Gexpr = NonterminalExpressions.cell_assigment_expression;
            Parser<char> input = "A0=SUM(A1)".ToCharArray();
            G.Initialize();
            G.Validate();
            G.Parse(input);
            bool expected = true;
            bool actual = (Gexpr[input.Tree] as Assignment).Expression == 0;
            Assert.AreEqual(expected, actual);
        }

        [TestMethod(), Conditional("DEBUG")]
        public void Test6_7()
        {
            Nonterminal<char> n = Nonterminals.cell_reference_group;
            Expression<char, CellExpression> nexpr = NonterminalExpressions.cell_reference_group;
            Nonterminal<char> G = "G";
            Expression<char, CellExpression> Gexpr = "G";
            G.Definition = new Rules<char>()
            {
                () => n[false] + Terminal<char>.Default
            };
            Gexpr.Eval = (a, b) =>
            {
                if (b[0].Context.Token.Equals(n))
                {
                    return nexpr[a, b[0]];
                }
                throw new NotSupportedException("Input token is not supported");
            };

            G.Initialize();
            G.Validate();

            Parser<char> input = "A1:A2".ToCharArray();
            bool expected = true;
            bool actual = G.Parse(input);
            Assert.AreEqual(expected, actual);
        }

        [TestMethod(), Conditional("DEBUG")]
        public void Test6_8()
        {
            Nonterminal<char> G = "G";
            Nonterminal<char> Gopt = "Gopt";
            Nonterminal<char> G1 = "G1";
            Nonterminal<char> G2 = "G2";
            G.Definition = new Rules<char>()
            {
                () => G2[false] + Terminal<char>.Default,
            };
            G2.Definition = new Rules<char>() 
            {
                () => G1[false] + Gopt[true],
            };
            G1.Definition = new Rules<char>() 
            {
                () => 'a',
                () => 'b'
            };
            Gopt.Definition = new Rules<char>() 
            {
                () => ':' + G1[false]
            };

            G.Initialize();
            G.Validate();

            Parser<char> input = "a:b".ToCharArray();
            bool expected = true;
            bool actual = G.Parse(input);
            Assert.AreEqual(expected, actual);
        }

        [TestMethod(), Conditional("DEBUG")]
        public void Test6_9()
        {
            Nonterminal<char> n = Nonterminals.cell_reference;
            Expression<char, CellExpression> nexpr = NonterminalExpressions.cell_reference;
            Nonterminal<char> G = "G";
            Expression<char, CellExpression> Gexpr = "G";
            G.Definition = new Rules<char>()
            {
                () => n[false] + Terminal<char>.Default
            };
            Gexpr.Eval = (a, b) =>
            {
                if (b[0].Context.Token.Equals(n))
                {
                    return nexpr[a, b[0]];
                }
                throw new NotSupportedException("Input token is not supported");
            };

            G.Initialize();
            G.Validate();

            Parser<char> input = "A1".ToCharArray();
            bool expected = true;
            bool actual = G.Parse(input);
            Assert.AreEqual(expected, actual);
        }

        [TestMethod(), Conditional("DEBUG")]
        public void Test6_10()
        {
            Nonterminal<char> n = Nonterminals.real_literal;
            Expression<char, CellExpression> nexpr = "real-literal[t]";
            Nonterminal<char> G = "G";
            Expression<char, CellExpression> Gexpr = "G";
            G.Definition = new Rules<char>()
            {
                () => n[false] + Terminal<char>.Default
            };
            Gexpr.Eval = (a, b) =>
            {
                if (b[0].Context.Token.Equals(n))
                {
                    return nexpr[a, b[0]];
                }
                throw new NotSupportedException("Input token is not supported");
            };

            G.Initialize();
            G.Validate();

            Parser<char> input = ".5".ToCharArray();
            bool expected = true;
            bool actual = G.Parse(input);
            Assert.AreEqual(expected, actual);
        }

        [TestMethod(), Conditional("DEBUG")]
        public void Test6_11()
        {
            Expression<char, CellExpression> nexpr = "rnd";
            bool expected = true;
            bool actual = nexpr.Name == "rnd";
            Assert.AreEqual(expected, actual);
        }
    }
}
